#include "iostream"
using namespace std;
struct time
{
	unsigned int ti_hour : 6;
	unsigned int ti_min : 6;
	unsigned int ti_sec : 6;
	unsigned int ti_hund : 6;

};
void gettime(time t)
{
	cout << "Nhap thoi gian: "; cin >> t.ti_hour;
	cout << "Nhap thoi gian: "; cin >> t.ti_min;
	cout << "Nhap thoi gian: "; cin >> t.ti_sec;
	cout << "Nhap thoi gian: "; cin >> t.ti_hor;
}
int tg_hund()

{
	time t;
	gettime(t);
	int s = ((t.ti_hour * 60 + t.ti_min) * 60 + t.ti_sec) * 100 + t.ti_hund;
	return s;
} char*p;

class CONVAT
{
private:
	int x, y;
	int mau;
	int rx, ry;
public:
	CONVAT(int x1, int y1, int mau1, int rx1, int ry1)
	{
		x = x1; y = y1; mau = mau1; rx = rx1; ry = ry1;
		char*f0;
		int size;
		size = imagesize(x1 - rx1, y1 - 2 * ry1, x1 + rx1, y1);
		f0 = (char*)malloc(size);
		p = (char*)malloc(size);
		getimage(x1 - rx1, y1 - 2 * ry1, x1 + rx1, y1, f0);
		int mau_ht = getcolor();
		setcolor(mau1);
		setfillstyle(SOLID_FILL, mau1);
		fillellipse(x1, y1 - ry, rx, ry);
		getimage(x1 - rx1, y1 - 2 * ry1, x1 + rx1, y1, p);
		putimage(x1 - rx1, y1 - 2 * ry1, f0, COPY_PUT);
		delete f0;
		f0 = NULL;
		setcolor(mau_ht);
	}
	~CONVAT()
	{
		delete p;
	}
	void hien()
	{
		putimage(x - rx, y - 2 * ry, p, XOR_PUT);
	}
	void an()
	{
		putimage(x - rx, y - 2 * ry, p, XOR_PUT);
	}
	int getx()
	{
		return x;
	}
	int gety()
	{
		return y;
	}
	void updatex(int x1)
	{
		x = x1;
	}
	void updatey(int y1)
	{
		y = y1;
	}
	void move()
	{
		x += 10;
	}
	void update(char key)
	{
		if (key == int('n'))
		{
			CONVAT()
		}
	}
};

class TRUNG
{
private:
	int x, y;
	int mau;
	int rx, ry;
public:
	TRUNG(int x1, int y1, int mau1, int rx1, int ry1)
	{
		x = x1; y = y1; mau = mau1; rx = rx1; ry = ry1;
		char*f0;
		int size;
		size = imagesize(x1 - rx1, y1 - 2 * ry1, x1 + rx1, y1);
		f0 = (char*)malloc(size);
		p = (char*)malloc(size);
		getimage(x1 - rx1, y1 - 2 * ry1, x1 + rx1, y1, f0);
		int mau_ht = getcolor();
		setcolor(mau1);
		setfillstyle(SOLID_FILL, mau1);
		fillellipse(x1, y1 - ry, rx, ry);
		getimage(x1 - rx1, y1 - 2 * ry1, x1 + rx1, y1, p);
		putimage(x1 - rx1, y1 - 2 * ry1, f0, COPY_PUT);
		delete f0;
		f0 = NULL;
		setcolor(mau_ht);
	}
	~TRUNG()
	{
		delete p;
	}
	void hien()
	{
		putimage(x - rx, y - 2 * ry, p, XOR_PUT);
	}
	void an()
	{
		putimage(x - rx, y - 2 * ry, p, XOR_PUT);
	}
	int getx()
	{
		return x;
	}
	int gety()
	{
		return y;
	}
	void updatex(int x1)
	{
		x = x1;
	}
	void updatey(int y1)
	{
		y = y1;
	}
	void move()
	{
		y += 10;
	}
};
class RO
{
private:
	int x, y;
	int r;
	int mau;
	char*tp;
public:
	RO(int x1, int y1, int mau1, int r1)
	{
		x = x1; y = y1; mau = mau1; r = r1;
		char*f;
		int size;
		size = imagesize(x1 - r1, y1, x1 + r1, y1 + r1);
		f = (char*)malloc(size);
		tp = (char*)malloc(size);
		getimage(x1 - r1, y1, x1 + r1, y1 + r1, f);
		int mau_ht = getcolor();
		setcolor(mau1);
		setfillstyle(SOLID_FILL, mau1);
		sector(x1, y1, 180, 360, r1, r1);
		getimage(x1 - r1, y1, x1 + r1, y1 + r1, tp);
		putimage(x1 - r1, y1, f, COPY_PUT);
		delete f;
		f = NULL;
		setcolor(mau_ht);
	}
	~RO()
	{
		delete tp;
	}
	int getx()
	{
		return x;
	}
	int gety()
	{
		return y;
	}
	void hien()
	{
		putimage(x - r, y, tp, XOR_PUT);
	}
	void an()
	{
		putimage(x - r, y, tp, XOR_PUT);
	}
	void update(char key)
	{
		if (key == 77)
		{
			if (x + r <= getmaxx() - 50)
				x += 2 * r;
		}
		else if (key == 75)
		{
			if (x - r >= 50)
				x -= 2 * r;
		}
	}
};
void play()
{
	setwritemode(XOR_PUT);
	int level;
	int lrot = 0;
	int xt1 = getmaxx() - 10, yt1 = getmaxy() - 10;
	int hang = 0;
	int cap = 5;
	setcolor(LIGHTGREEN);
	settextstyle(0, 0, 3); settextjustify(1, 1);
	outtextxy(getmaxx() / 2, 50, "Tro choi hung trung");
	settextstyle(0, 0, 0);
	int diem = 0, diemt = 0, xt = 50;
	int yt = getmaxy() - 50;
	char string[10];
	setcolor(12);
	itoa(diem, string, 10);
	outtextxy(textwidth("Diem:"), 10, "Diem:");
	outtextxy(2 * textwidth("Diem:"), 10, string);
	char key;
	RO a(getmaxx() / 2, getmaxy() - 150, BROWN, 20);
	TRUNG b(getmaxx() / 2, getmaxy() / 2, YELLOW, 5, 10);
	setbkcolor(BLUE);
	a.hien();
	randomize();
	int x;
	x = random(getmaxx() - 50);
	if (x <= 50) x += 50;
	b.updatey(0);
	b.updatex(x);
	b.hien();
	int tg_bd = tg_hund();
	while (1)
	{
		int tg = cap;
		int tg_ht = tg_hund();
		if ((tg_ht - tg_bd)<tg)
		{
			if (kbhit())
			{
				key = getch();
				a.an();
				a.update(key);
				a.hien();
			}
		}
		else
		{
			b.an();
			b.move();
			b.hien();
			if (b.gety() >= getmaxy())
			{
				b.an();
				x = random(getmaxx() - 50);
				if (x <= 50) x += 50;
				b.updatey(0);
				b.updatex(x);
				b.hien();
				lrot++;
			}
			tg_bd = tg_hund();
		}
		char ans;
		if (lrot == 5)
		{
			settextstyle(0, 0, 3); settextjustify(1, 1);
			do{
				setcolor(LIGHTRED);
				outtextxy(getmaxx() / 2, getmaxy() / 2, "Game over(Y/N)");
				if (kbhit()) ans = toupper(getch());
				if (ans == 'N') exit(1);
				else if (ans == 'Y')
				{
					cleardevice();
					play();
					break;
				}
				delay(100);
				setcolor(BLUE);
				outtextxy(getmaxx() / 2, getmaxy() / 2, "Game over(Y/N)");
				delay(100);
			} while (ans != 'Y'&&ans != 'N');
		}//if
		if (key == 27) break;
		if ((b.getx() <= a.getx() + 20) && (b.getx() >= a.getx() - 20) &&
			(((b.gety() - 5) >= a.gety()) && ((b.gety() - 5) <= a.gety() + 10)))
		{
			b.an();
			x = random(getmaxx() - 50);
			if (x <= 50) x += 50;
			b.updatey(0);
			b.updatex(x);
			b.hien();
			setcolor(BLUE);
			outtextxy(2 * textwidth("Diem:"), 10, string);
			setcolor(12);
			diem++;
			if (cap>1)
			{
				if (diemt >= 5)
				{
					cap--;
					diemt = 0;
				}
				else
					diemt++;
			}
			itoa(diem, string, 10);
			outtextxy(2 * textwidth("Diem:"), 10, string);
			setcolor(YELLOW);
			setfillstyle(SOLID_FILL, YELLOW);
			fillellipse(xt, yt, 5, 10);
			if (xt >= getmaxx() - 50)
			{
				yt -= 24;
				xt = 50;
				hang++;
			}
			else
				xt += 15;
			sound(1000); delay(50); nosound();
			if (hang == 2)
			{
				setcolor(BLUE);
				setfillstyle(SOLID_FILL, BLUE);
				fillellipse(xt1, yt1, 5, 10);
				for (yt = getmaxy() - 50; yt >= getmaxy() - 74; yt -= 24)
					for (xt = 50; xt <= getmaxx() - 20; xt += 15)
						fillellipse(xt, yt, 5, 10);
				setcolor(RED);
				setfillstyle(SOLID_FILL, RED);
				fillellipse(xt1, yt1, 5, 10);
				yt1 -= 24;
				hang = 0;
				xt = 50;
				yt = getmaxy() - 50;
			}
		}
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	play();
	return 0;
}

